package santiago.view.lobby;

import java.util.Set;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import santiago.client.Client;
import santiago.domain.Game;
import santiago.view.SantiagoFrame;

public class FrameServerMenu extends SantiagoFrame {

    private int SelectedGameIndex;

    /**
     * Creates new form FrameServerMenu
     */
    public FrameServerMenu() {
        super();
        initComponents();
        refresh();
        setLocationRelativeTo(null);
    }

    @Override
    public void refresh() {
        ButtonJoinGame.setEnabled(false);
        TableGames = new JTable() {
            private static final long serialVersionUID = 1L;

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        TableGames.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        String[][] Data = null;
        String Col[] = {"Nom de la partie", "Nombre de joueurs"};
        DefaultTableModel Model = new DefaultTableModel(Data, Col);
        Set<Game> Games = Client.getGames();
        for (Game Game : Games) {
            Model.addRow(new String[]{Game.getName(), Game.getCurrentPlayerCount() + "/" + Game.getParameters().getPlayerCount()});
        }
        TableGames.setModel(Model);
        ScrollPaneTableContainer.getViewport().removeAll();
        ScrollPaneTableContainer.getViewport().add(TableGames);
        TableGames.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                gameSelected(e);
            }
        });
    }

    public void gameSelected(ListSelectionEvent e) {
        ListSelectionModel SelectionModel = (ListSelectionModel) e.getSource();
        if (!SelectionModel.isSelectionEmpty()) {
            SelectedGameIndex = SelectionModel.getMinSelectionIndex();
            ButtonJoinGame.setEnabled(true);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ButtonCreateGame = new javax.swing.JButton();
        ButtonJoinGame = new javax.swing.JButton();
        ButtonRefreshList = new javax.swing.JButton();
        ButtonDisconnect = new javax.swing.JButton();
        ScrollPaneTableContainer = new javax.swing.JScrollPane();
        TableGames = new javax.swing.JTable();
        LabelBackground = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Santiago : Lobby");
        setMinimumSize(new java.awt.Dimension(1024, 760));
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        ButtonCreateGame.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        ButtonCreateGame.setForeground(new java.awt.Color(179, 96, 74));
        ButtonCreateGame.setText("Créer une partie");
        ButtonCreateGame.setBorder(null);
        ButtonCreateGame.setFocusPainted(false);
        ButtonCreateGame.setMaximumSize(new java.awt.Dimension(400, 30));
        ButtonCreateGame.setMinimumSize(new java.awt.Dimension(400, 30));
        ButtonCreateGame.setOpaque(false);
        ButtonCreateGame.setPreferredSize(new java.awt.Dimension(400, 30));
        ButtonCreateGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonCreateGameActionPerformed(evt);
            }
        });
        getContentPane().add(ButtonCreateGame, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 200, 190, -1));

        ButtonJoinGame.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        ButtonJoinGame.setForeground(new java.awt.Color(179, 96, 74));
        ButtonJoinGame.setText("Rejoindre");
        ButtonJoinGame.setBorder(null);
        ButtonJoinGame.setEnabled(false);
        ButtonJoinGame.setFocusPainted(false);
        ButtonJoinGame.setMaximumSize(new java.awt.Dimension(400, 30));
        ButtonJoinGame.setMinimumSize(new java.awt.Dimension(400, 30));
        ButtonJoinGame.setOpaque(false);
        ButtonJoinGame.setPreferredSize(new java.awt.Dimension(400, 30));
        ButtonJoinGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonJoinGameActionPerformed(evt);
            }
        });
        getContentPane().add(ButtonJoinGame, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 200, 190, -1));

        ButtonRefreshList.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        ButtonRefreshList.setForeground(new java.awt.Color(179, 96, 74));
        ButtonRefreshList.setText("Rafraîchir la liste");
        ButtonRefreshList.setBorder(null);
        ButtonRefreshList.setFocusPainted(false);
        ButtonRefreshList.setMaximumSize(new java.awt.Dimension(400, 30));
        ButtonRefreshList.setMinimumSize(new java.awt.Dimension(400, 30));
        ButtonRefreshList.setOpaque(false);
        ButtonRefreshList.setPreferredSize(new java.awt.Dimension(400, 30));
        ButtonRefreshList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonRefreshListActionPerformed(evt);
            }
        });
        getContentPane().add(ButtonRefreshList, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 550, 190, -1));

        ButtonDisconnect.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        ButtonDisconnect.setForeground(new java.awt.Color(179, 96, 74));
        ButtonDisconnect.setText("Déconnexion");
        ButtonDisconnect.setBorder(null);
        ButtonDisconnect.setFocusPainted(false);
        ButtonDisconnect.setMaximumSize(new java.awt.Dimension(400, 30));
        ButtonDisconnect.setMinimumSize(new java.awt.Dimension(400, 30));
        ButtonDisconnect.setOpaque(false);
        ButtonDisconnect.setPreferredSize(new java.awt.Dimension(400, 30));
        ButtonDisconnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonDisconnectActionPerformed(evt);
            }
        });
        getContentPane().add(ButtonDisconnect, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 550, 190, -1));

        ScrollPaneTableContainer.setFont(new java.awt.Font("Lucida Sans", 0, 14)); // NOI18N
        ScrollPaneTableContainer.setOpaque(false);

        TableGames.setFont(new java.awt.Font("Lucida Sans", 0, 14)); // NOI18N
        TableGames.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        TableGames.getTableHeader().setResizingAllowed(false);
        ScrollPaneTableContainer.setViewportView(TableGames);

        getContentPane().add(ScrollPaneTableContainer, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 240, 600, 290));

        LabelBackground.setBackground(new java.awt.Color(138, 192, 208));
        LabelBackground.setIcon(new javax.swing.ImageIcon(getClass().getResource("/santiago/ressources/fond-menu-principal.png"))); // NOI18N
        LabelBackground.setMaximumSize(new java.awt.Dimension(1024, 760));
        LabelBackground.setMinimumSize(new java.awt.Dimension(1024, 760));
        LabelBackground.setPreferredSize(new java.awt.Dimension(1024, 760));
        getContentPane().add(LabelBackground, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));

        setSize(new java.awt.Dimension(1024, 760));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void ButtonCreateGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonCreateGameActionPerformed
        Client.getInterface().createGame();
    }//GEN-LAST:event_ButtonCreateGameActionPerformed

    private void ButtonJoinGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonJoinGameActionPerformed
        String GameName = TableGames.getModel().getValueAt(SelectedGameIndex, 0).toString();
        Client.joinGame(GameName);
    }//GEN-LAST:event_ButtonJoinGameActionPerformed

    private void ButtonRefreshListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonRefreshListActionPerformed
        refresh();
    }//GEN-LAST:event_ButtonRefreshListActionPerformed

    private void ButtonDisconnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonDisconnectActionPerformed
        Client.disconnect();
    }//GEN-LAST:event_ButtonDisconnectActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ButtonCreateGame;
    private javax.swing.JButton ButtonDisconnect;
    private javax.swing.JButton ButtonJoinGame;
    private javax.swing.JButton ButtonRefreshList;
    private javax.swing.JLabel LabelBackground;
    private javax.swing.JScrollPane ScrollPaneTableContainer;
    private javax.swing.JTable TableGames;
    // End of variables declaration//GEN-END:variables
}
